<template>
  <el-dialog v-model="visible" title="新建操作员" :before-close="handleClose">
    <el-form :model="form" label-width="120px">
      <el-form-item label="头像">
        <el-upload
            class="avatar-uploader"
            :action="imageBaseUrl()+'/lms/file'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <el-button type="primary">Click to upload</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="用户名">
        <el-input v-model="form.username"/>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" show-password/>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="form.name"/>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="form.phone"/>
      </el-form-item>
      <el-form-item label="所属仓库">
        <el-input v-model="form.wid"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="sureEditAccount">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import {reactive} from "vue";
import {createOperator} from "@/scripts/data/operator";
import {imageBaseUrl} from "@/scripts/data/image";

export default {
  name: "OperatorCreate",
  props: ['callback'],
  data() {
    return {
      visible: false,
      change: false,
      imageUrl:null,
      form: reactive({
        username: '',
        password: '',
        name: '',
        phone: '',
        wid: '',
        oid: '',
      })
    }
  },
  methods: {
    imageBaseUrl() {
      return imageBaseUrl
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.avatar = res.msg;
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return  isLt2M;
    },
    sureEditAccount() {
      this.change = true;
      createOperator(this.form, this.callback)
    },
    handleClose() {
      this.visible = false;
      if (!this.change) {
        this.callback(-1);
      }
    },
  },
  mounted() {
    this.visible = true;
  }
}
</script>

<style scoped>


.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

</style>
